package net.yxsoft.kit;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 中文日期
 *
 * @author wj
 * @Created by 16 on 2019/4/4.
 * @since 1.0
 */

public class ChineseDateUtil {
    /** 大写数字 */

    private static final String[] NUMBERS = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };

    private static final String[] data = { "年","月","日"};

    /** 通过 yyyy-MM-dd 得到中文大写格式 yyyy MM dd 日期 */

    public static synchronized String toChinese(String str) {

        StringBuffer sb = new StringBuffer();

        sb.append(getSplitDateStr(str, 0)).append(" ").append(

                getSplitDateStr(str, 1)).append(" ").append(

                getSplitDateStr(str, 2));

        return sb.toString();

    }

    public static String getSplitDateStr(String str,int num){

        String[] DateStr = str.split("-");

        StringBuffer sb = new StringBuffer();

        for(int i=0;i<DateStr[num].length();i++){

            sb.append(countnum(DateStr[num].substring(i, i + 1)));

        }

        return sb.append(data[num]).toString();

    }

    public static String countnum(String num){

        return NUMBERS[Integer.valueOf(num)];

    }


    /**
     * 获取唯一时间戳
     * @return
     */
    public static Long getNextNaos(){
        AtomicLong timeMills = new AtomicLong(0);
        while (true){
            long currentTimeMillis = System.nanoTime();
            long currentMill = timeMills.get();
            if(currentTimeMillis > currentMill && timeMills.compareAndSet(currentMill, currentTimeMillis)){
                return currentTimeMillis;
                //返回唯一时间戳
            }
        }
    }

}
